<?php namespace Phpcmf\Controllers\Admin;

class Son extends \Phpcmf\Admin\Mform
{

    public function index() {

        $tid = dr_safe_filename($_GET['tid']);
		$cid = $_GET['cid'];
		$yibie = $_GET['jibie'];
		if($yibie==0 || $yibie==''){
			$daohang1 = [];
		} elseif ($yibie==1) {
			$daohang_1 = \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_form_son_daohang')->where('cid', $cid)->where('yijiid', $tid)->getAll();
			$daohang1 = [];
			//循环
			foreach ($daohang_1 as $i => $t) {
			    $daohang1[] = $t['id'];
			}
		} elseif ($yibie==2) {
			$daohang_1 = \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_form_son_daohang')->where('id', $tid)->getAll();
			$daohang1 = [];
			//循环
			foreach ($daohang_1 as $i => $t) {
			    $daohang1[] = $t['id'];
			}
		}
		
        if (strlen($tid)) {
            $tid = intval($tid);
            if (!empty($daohang1)) {
                $this->init['where_list'].= ' and tid IN ('.implode(',', $daohang1).')';
            } else {
                $this->init['where_list'].= ' and tid='.$tid;
            }
        }

        $this->init['order_by'] = 'displayorder ASC,inputtime ASC';

        $p = [
            'cid' =>  $this->cid,
            'tid' =>  $tid,
			'yijiid' => $cid
        ];
		
		//$this->init['where_list'].= 'and yijiid = '.$cid;

        $this->_List($p);

        $stype = \Phpcmf\Service::M('content', 'shouce')->son_type($this->index['son_type']);
		$daohang = \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_form_son_daohang')->where('cid', $cid)->getAll();
		$daohang_2 = \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_form_son_daohang')->where('yijiid', $tid)->where('yijiid', 0)->getAll();

        \Phpcmf\Service::V()->assign([
            'p' => $p,
            'stype' => $stype,
			'daohang1' => $daohang1,
			'daohang' => $daohang,
			'daohang_2' => $daohang_2,
            'clink' => $this->_app_clink('mform'),
            'cbottom' => $this->_app_cbottom('mform'),
            'sname' => strlen($tid) ? $stype[$tid] : '全部',
            'menu' => \Phpcmf\Service::V()->get_value('menu').'<li> <a  href="javascript:dr_help(\'628\');"><i class="fa fa-question-circle"></i> '.dr_lang('在线帮助').'</a> <i class="fa fa-circle"></i> </li>',
        ]);
        \Phpcmf\Service::V()->display('son_list.html');
    }
	public function list_son(){
		\Phpcmf\Service::V()->display('son_list_data.html');
	}

    public function add() {
        $this->_Admin_Add();
    }

    public function edit() {
        $this->_Admin_Edit();
    }

    public function show_index() {
        $this->_Admin_Show();
    }

    public function order_edit() {
        $this->_Admin_Order();
    }

    public function del() {
        $this->_Admin_Del();
    }


    // 隐藏或者启用
    public function vip_edit() {

        $id = (int)\Phpcmf\Service::L('Input')->get('id');
        $row =\Phpcmf\Service::M()->table($this->init['table'])->get($id);
        !$row && $this->_json(0, dr_lang('数据不存在'));

        $v = $row['vip'] ? 0 : 1;
        \Phpcmf\Service::M()->table($this->init['table'])->update($id, ['vip' => $v]);

        exit($this->_json(1, dr_lang($v ? '已设置收费' : '已设置免费'), ['value' => $v]));
    }

    // 分类操作
    public function son_add() {

        if (IS_POST) {
            $name = dr_safe_replace($_POST['name']);
            if (!$name) {
                $this->_json(0, dr_lang('名称没有填写'));
            }
            $rt = \Phpcmf\Service::M('content', 'shouce')->add_son_type($this->index, $name);
            if (!$rt['code']) {
                $this->_json(0, $rt['msg']);
            }
            $this->_json(1, dr_lang('操作成功'));
        }

        \Phpcmf\Service::V()->assign([
            'value' => '',
        ]);
        \Phpcmf\Service::V()->display('son_name.html');exit;
    }


    public function son_edit() {

        $tid = intval($_GET['tid']);
        $stype = \Phpcmf\Service::M('content', 'shouce')->son_type($this->index['son_type']);

        if (IS_POST) {
            $name = dr_safe_replace($_POST['name']);
            !$name && $this->_json(0, dr_lang('名称没有填写'));
            $stype[$tid] = $name;
            \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR))->update($this->cid, [
               'son_type' => dr_array2string($stype)
            ]);
            $this->_json(1, dr_lang('操作成功'));
        }

        \Phpcmf\Service::V()->assign([
            'value' => $stype[$tid],
        ]);
        \Phpcmf\Service::V()->display('son_name.html');exit;

    }

    public function son_del() {

        $tid = intval($_GET['tid']);
        $stype = \Phpcmf\Service::M('content', 'shouce')->son_type($this->index['son_type']);
        !$stype[$tid] && $this->_json(0, dr_lang('分类不存在'));
        unset($stype[$tid]);
        \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR))->update($this->cid, [
            'son_type' => dr_array2string($stype)
        ]);

        $data = \Phpcmf\Service::M()->table($this->init['table'])->where('cid', $this->cid)->where('tid', $tid)->getAll();
        if ($data) {
            foreach ($data as $t) {
                $_POST['ids'][] = $t['id'];
            }
            $this->del();
        }

        $this->_json(1, dr_lang('操作成功'));

    }
	// 一级分类操作
		public function son_add_yiji() {
			$cid = intval($_GET['cid']);
			
			//判断是否是圈子的圈主
			$quanzhuid = \Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR))->where('id',$cid)->where('uid',$this->uid)->get()->getRowArray();
			// 导航数量
			/*$erjishuliang = \Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR).'_form_son_daohang')->where('cid', $cid)->where('yijiid', $yijiid)->countAllResults();*/
			$user =dr_member_info($this->uid);
	        $admin = $user['is_admin'];
			if (!$quanzhuid && !$admin) {
				$this->_json(1, dr_lang('您不是这个圈的圈主,无法添加分类'));
			}
			//$erjiid = $yijiid*100+$erjishuliang+1;
			if (IS_POST) {
				
				$name = dr_safe_replace($_POST['name']);
				$yincang = dr_safe_replace($_POST['yincang']);
				$fabu = dr_safe_replace($_POST['fabu']);
				$yanse = dr_safe_replace($_POST['yanse']);
				$paixu = dr_safe_replace($_POST['paixu']);
				$moren = dr_safe_replace($_POST['moren']);
				\Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR).'_form_son_daohang')->insert(array(
				    'cid' => $cid,
				    'uid' => $this->uid,
					'yijiid' => 0,
					'tieid' => 0,
					'name' => $name,
					'yincang' => $yincang,
					'fabu' => $fabu,
					'yanse' => $yanse,
					'paixu' => $paixu,
					'moren' => $moren,
					'inputtime' => SYS_TIME,
				));
				
				$this->_json(1, dr_lang('操作成功'));
			}
			\Phpcmf\Service::V()->display('son_name_yiji.html');exit;
		}
		//一级分类修改
		public function son_edit_yiji() {
			$id = intval($_GET['id']);
			$tieid = intval($_GET['tieid']);
			$stype = \Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR).'_form_son_daohang')->where('id',$id)->get()->getRowArray();
			$user =dr_member_info($this->uid);
	        $admin = $user['is_admin'];
			if(!$admin && !$this->uid) {
				$this->_json(0, dr_lang('抱歉不是你的圈子，无法修改分类'));
			}
			if (IS_POST) {
				$name = dr_safe_replace($_POST['name']);
				$yincang = dr_safe_replace($_POST['yincang']);
				$fabu = dr_safe_replace($_POST['fabu']);
				$yanse = dr_safe_replace($_POST['yanse']);
				$paixu = dr_safe_replace($_POST['paixu']);
				$moren = dr_safe_replace($_POST['moren']);
				\Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_form_son_daohang')->update($id,[
				    'name' => $name,
				    'yincang' => $yincang,
				    'fabu' => $fabu,
				    'yanse' => $yanse,
				    'paixu' => $paixu,
				    'moren' => $moren,
				    //'inputtime' => SYS_TIME,
				]);
				$this->_json(1, dr_lang('修改操作成功'));
			}
			\Phpcmf\Service::V()->assign([
			    'value' => $stype,
			]);
			\Phpcmf\Service::V()->display('son_name_yiji.html');exit;
		}
		//一级分类删除
		public function son_del_yiji() {
			//圈子id
			$id = intval($_GET['id']);
			$tieid = intval($_GET['tieid']);
			$stype = \Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR).'_form_son_daohang')->where('id',$id)->get()->getRowArray();
			$user =dr_member_info($this->uid);
	        $admin = $user['is_admin'];
			if(!$admin && !$this->uid) {
				$this->_json(0, dr_lang('抱歉不是你的圈子，无法修改分类'));
			}
			$erjifenlei = \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_form_son_daohang')->where('yijiid',$id)->getAll();
			if ($erjifenlei) {
				$this->_json(1, dr_lang('分类下面有二级分类，不能删除,先删除二级分类'));
			}
			$neirong = \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_form_son')->where('tid',$id)->getAll();
			if ($neirong) {
				$this->_json(1, dr_lang('分类下面有帖子，不能删除'));
			}
	
			\Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_form_son_daohang')->where('id',$id)->where('uid',$this->uid)->delete();
			$this->_json(1, dr_lang('操作成功'));
		}
	// 二级分类添加
		public function son_add_erji() {
			$id = intval($_GET['id']);
			$cid = intval($_GET['cid']);
			$daid = \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_form_son_daohang')->where('id',$cid)->getAll();
			$erjiids= max($daid);
			//dr_debug('max', $erjiids['id']);
			$yijiid = intval($_GET['yijiid']);
			//判断是否是圈子的圈主
			$quanzhuid = \Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR))->where('id',$cid)->get()->getRowArray();
			// 导航数量
			$erjishuliang = \Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR).'_form_son_daohang')->where('cid', $cid)->where('yijiid', $yijiid)->countAllResults();
			$user =dr_member_info($this->uid);
	        $admin = $user['is_admin'];
			if (!$admin && !$this->uid) {
				$this->_json(1, dr_lang('您不是这个圈的圈主,无法添加二级分类'));
			}
			$erjiid = $id*100+$erjishuliang+1;
			if (IS_POST) {
				
				$name = dr_safe_replace($_POST['name']);
				$yincang = dr_safe_replace($_POST['yincang']);
				$fabu = dr_safe_replace($_POST['fabu']);
				$yanse = dr_safe_replace($_POST['yanse']);
				$paixu = dr_safe_replace($_POST['paixu']);
				$moren = dr_safe_replace($_POST['moren']);
				\Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR).'_form_son_daohang')->insert(array(
				    'cid' => $cid,
				    'uid' => $this->uid,
				    'yijiid' => $id,
				    'tieid' => $erjiid,
				    'name' => $name,
				    'yincang' => $yincang,
				    'fabu' => $fabu,
				    'yanse' => $yanse,
				    'paixu' => $paixu,
				    'moren' => $moren,
				    'inputtime' => SYS_TIME,
				));
				
				$this->_json(1, dr_lang('操作成功'));
			}
			\Phpcmf\Service::V()->display('son_name_yiji.html');exit;
		}
		//二级分类修改
		public function son_edit_erji() {
			$id = intval($_GET['id']);
			$tieid = intval($_GET['tieid']);
			$stype = \Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR).'_form_son_daohang')->where('id',$id)->get()->getRowArray();
			$user =dr_member_info($this->uid);
	        $admin = $user['is_admin'];
			if (!$admin && !$this->uid){
				$this->_json(0, dr_lang('您不是这个圈的圈主,无法修改分类'));
			}
			if (IS_POST) {
				$name = dr_safe_replace($_POST['name']);
				$yincang = dr_safe_replace($_POST['yincang']);
				$fabu = dr_safe_replace($_POST['fabu']);
				$yanse = dr_safe_replace($_POST['yanse']);
				$paixu = dr_safe_replace($_POST['paixu']);
				$moren = dr_safe_replace($_POST['moren']);
				\Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_form_son_daohang')->update($id,[
				    'name' => $name,
				    'yincang' => $yincang,
				    'fabu' => $fabu,
				    'yanse' => $yanse,
				    'paixu' => $paixu,
				    'moren' => $moren,
				]);
				$this->_json(1, dr_lang('修改操作成功'));
			}
			\Phpcmf\Service::V()->assign([
			    'value' => $stype,
			]);
			\Phpcmf\Service::V()->display('son_name_yiji.html');exit;
		}
		//二级分类删除
		public function son_del_erji() {
			//圈子id
			$id = intval($_GET['id']);
			$tieid = intval($_GET['tieid']);
			$neirong = \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_form_son')->where('cid',$tieid)->where('tid',$id)->getAll();
			$user =dr_member_info($this->uid);
	        $admin = $user['is_admin'];
			if ($neirong) {
				$this->_json(1, dr_lang('分类下面有帖子，不能删除'));
			}
			$stype = \Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR).'_form_son_daohang')->where('id',$id)->get()->getRowArray();
			if (!$admin && !$this->uid){
				$this->_json(0, dr_lang('您不是这个圈的圈主,无法删除分类'));
			}
			\Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_form_son_daohang')->where('id',$id)->delete();
			$this->_json(1, dr_lang('操作成功'));
		}
}
